<template>
  <el-menu :default-active="defaultActive" :router="router" v-bind="$attrs" class="el-menu-vertical-demo">
    <template v-for="(item, index) in data" :key="index">
      <el-menu-item v-if="!item[children] || !item[children].length" :index="item.index" @click="handleClick(item.index)">
        <component v-if="item[icon]" :is='`el-icon-${toLine(item[icon])}`' class="icon"></component>
        <span>{{ item[name] }}</span>
      </el-menu-item>
      <el-sub-menu v-if="item[children] && item[children].length" :index="item.index">
        <template #title>
          <component v-if="item[icon]" :is='`el-icon-${toLine(item[icon])}`' class="icon"></component>
          <span>{{ item[name] }}</span>
        </template>
        <el-menu-item v-for="(item1, index1) in item[children]" :index="item1[index]" :key="index1" @click="handleClick(item.index)">
          <component v-if="item1[icon]" :is='`el-icon-${toLine(item1[icon])}`' class="icon"></component>
          <span>{{ item1[name]}}</span>
        </el-menu-item>
      </el-sub-menu>
    </template>
  </el-menu>
</template>

<script setup lang="ts">
import { PropType } from "vue"
import { toLine } from "../../../utils";

let props = defineProps({
  data: {
    type: Array as PropType<any[]>,
    required: true
  },
  //默认选中
  defaultActive: {
    type: String,
    default: ''
  },
  //是否是路由模式
  router: {
    type: Boolean,
    default: false
  },
  //键名
  //菜单标题的键名
  name: {
    type: String,
    default: 'name'
  },
  //菜单标识的键名
  index: {
    type: String,
    default: 'index'
  },
  //菜单图标的键名
  icon: {
    type: String,
    default: 'icon'
  },
  //子菜单的键名
  children: {
    type: String,
    default: 'children'
  },
})

const emits = defineEmits(['menu-click'])
const handleClick = (index: any) => {
  emits('menu-click',index)
}
</script>

<style scoped lang="scss">
.icon{
  margin: 0 20px;
}
svg {
  width: 1em;
  height: 1em;
}
.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 200px;
}
</style>